package com.my.study.pool;

import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * @author Carlos
 * @version 1.0
 * @Description TODO
 * @date 2021/6/13 21:05
 **/
@Slf4j(topic = "c.TestShutdown")
public class TestShutdown {

    public static void main(String[] args) throws Exception {
        ExecutorService pool = Executors.newFixedThreadPool(2);

//        testShutdown(pool);

        testShutdownNow(pool);

    }

    /**
     * Shutdown()方法
     * <p>
     * shutdown()方法执行后，会让线程池里的任务执行完毕，并且执行完任务队列的任务。
     * 但是再加任务，就会抛出异常。
     * 还有一点，shutdown()不会阻塞主线程后续操作，除非再调用awaitTermination()/get()
     *
     * @param pool
     */
    private static void testShutdown(ExecutorService pool) throws Exception {
        Future<Integer> r1 = pool.submit(() -> {
            log.debug("task 1 running...");
            Thread.sleep(1000);
            log.debug("task 1 finish...");
            return 1;
        });

        Future<Integer> r2 = pool.submit(() -> {
            log.debug("task 2 running...");
            Thread.sleep(1000);
            log.debug("task 2 finish...");
            return 2;
        });

        Future<Integer> r3 = pool.submit(() -> {
            log.debug("task 3 running...");
            Thread.sleep(1000);
            log.debug("task 3 finish...");
            return 3;
        });

        log.debug("shutdown...");
        pool.shutdown();
//        Future<Integer> r4 = pool.submit(() -> {
//            log.debug("task 4 running...");
//            Thread.sleep(1000);
//            log.debug("task 4 finish...");
//            return 4;
//        });
        pool.awaitTermination(2, TimeUnit.SECONDS);
        log.debug("r1 = {}", r1.get());
        log.debug("r2 = {}", r2.get());
        log.debug("r3 = {}", r3.get());
    }

    /**
     * ShutdownNow()
     * ShutdownNow()方法执行后，会打断正在运行的线程，也不会运行任务队列里面的任务
     * 但是会将任务队列中的任务作为结果返回
     *
     *
     * @param pool
     * @throws Exception
     */
    private static void testShutdownNow(ExecutorService pool) throws Exception {
        Future<Integer> r1 = pool.submit(() -> {
            log.debug("task 1 running...");
            Thread.sleep(1000);
            log.debug("task 1 finish...");
            return 1;
        });

        Future<Integer> r2 = pool.submit(() -> {
            log.debug("task 2 running...");
            Thread.sleep(1000);
            log.debug("task 2 finish...");
            return 2;
        });

        Future<Integer> r3 = pool.submit(() -> {
            log.debug("task 3 running...");
            Thread.sleep(1000);
            log.debug("task 3 finish...");
            return 3;
        });

        log.debug("shutdownNow...");
        List<Runnable> runnables = pool.shutdownNow();
        runnables.forEach(f -> {
            log.debug("被中断的任务队列中的任务：{}", f);
        });

//        log.debug("r1 = {}", r1.get());
//        log.debug("r2 = {}", r2.get());
//        log.debug("r3 = {}", r3.get());
    }

}
